package com.zrx.zrxpicturebackend.controller;

public class testMain {

    public static void main(String[] args) {
        // 情况1：字符串字面量
        String a = "hello";
        String b = "hello";
        String c = a.intern();

        System.out.println("a == b: " + (a == b)); // true - 都指向常量池
        System.out.println("a == c: " + (a == c)); // true - intern也指向常量池

        // 情况2：使用new创建字符串
        String d = new String("hello");
        String e = new String("hello");
        String f = d.intern();

        System.out.println("d == e: " + (d == e)); // false - 不同的对象实例
        System.out.println("d == f: " + (d == f)); // false - d是堆对象，f是常量池对象
        System.out.println("a == f: " + (a == f)); // true - f指向常量池中的"a"

        // 情况3：运行时构造的字符串
        String userId1 = String.valueOf(123);
        String userId2 = String.valueOf(123);
        String intern1 = userId1.intern();
        String intern2 = userId2.intern();

        System.out.println("userId1 == userId2: " + (userId1 == userId2)); // false - 不同对象
        System.out.println("intern1 == intern2: " + (intern1 == intern2)); // true - 同一个常量池引用
    }
}
